package com.fl.iocaop.java8.Teststream;

import com.fl.iocaop.java8.apple;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 终止操作
 * 查找与匹配
 * allMatch—检查是否匹配所有元素
 * anyMatch——检查是否至少匹配一个元素
 * noneMatch——检查是否没有匹配所有元素
 * findFirst——返回第一个元素
 * findAny——返回当前流中的任意元素
 * count——返回流中元素的总个数
 * max——返回流中最大值
 * min——返回流中最小值
 *
 */
public class TestStreamAPI3 {
    private List<apple> apples = TestStreamApI2.methodTest();
    /*
    判断apples集合中的状态是否都为Free
     */
    @Test
    public void test(){
        //判断apples集合中的状态是否都为Free
        boolean match = apples.stream().allMatch(e -> e.getStatus().equals(apple.status.FREE));
        System.out.println("match = " + match);

        // 获取aPPLE集合中重量最大的对象
        apple apple = apples.stream().max(Comparator.comparingInt(e -> e.getWeight())).get();
        System.out.println("apple = " + apple);

        //比较判断apples集合中重量最大的apple，并返回
        apple apple1 = apples.stream().sorted((e1, e2) -> Integer.compare(e2.getWeight(), e1.getWeight())).findFirst().get();
        System.out.println("apple1 = " + apple1);

    }
    @Test
    public void test2(){
        //获取apples集合中状态为成功的对象集合
        Optional<apple> any = apples.stream()
                .filter((e) -> e.getStatus().equals(apple.status.FREE))
                .findAny();
        System.out.println(any.get());

        //获取比apples集合中重量最小的apple，并返回
        System.out.println(apples.stream()
                .map(apple::getWeight)
                .min(Integer::compare));

        //获取apples集合中重量的总和  (规约：reduce)
        System.out.println("总重量:"+apples.stream()
                .map(apple::getWeight)
                .reduce(Integer::sum).get());
    }
    @Test
    public void test3(){
        /**
         * 收集
         * collect—将流转换为其他形式。接收一个Collector接口的实现，用于给Stream中元素做汇总的方法
         */
        //将apples集合的颜色取出存储在新的集合中
        List<String> collect = apples.stream()
                .map(apple::getColor)
                .distinct()
                .collect(Collectors.toList());
        System.out.println("collect = " + collect);

        //存储特殊的集合
        HashSet<String> set = apples.stream().map(apple::getColor).collect(Collectors.toCollection(HashSet::new));
        System.out.println("set = " + set);

        //集合总数
        long count = apples.stream().count();
        System.out.println("count = " + count);

        //平均重量
        Double Avgweigth = apples.stream().collect(Collectors.averagingInt(apple::getWeight));
        System.out.println("Avgweigth = " + Avgweigth);

        //总重量
        long sum = apples.stream().collect(Collectors.summarizingInt(apple::getWeight)).getSum();
        System.out.println("sum = " + sum);

        //最大重量
        apple ap = apples.stream().max(Comparator.comparingInt(apple::getWeight)).get();
        apple ap2 = apples.stream().collect(Collectors.maxBy((x, y) -> Integer.compare(x.getWeight(), y.getWeight()))).get();
        System.out.println("最大重量->apple = " + ap2);

        //最小重量
        apple app3 = apples.stream().min(Comparator.comparingInt(com.fl.iocaop.java8.apple::getWeight)).get();
        System.out.println("最小重量->app3 = " + app3);
    }
    @Test
    public void test4(){
        //按状态分组
        Map<apple.status, List<apple>> collect = apples.stream().collect(Collectors.groupingBy(apple::getStatus));
        collect.forEach((k,v)-> System.out.println(k+":"+v));
        //按是否大于50kg分
        Map<Boolean, List<apple>> map = apples.stream().collect(Collectors.partitioningBy(x -> x.getWeight() > 50));
        map.forEach((k,v)-> System.out.println(k+":"+v));

        //条件排序
        apples.stream().filter(x->x.getWeight()>60)
                .sorted((a,b)->a.getColor().compareTo(a.getColor()))
                .distinct()
                .map(apple::getColor).forEach(System.out::println);
    }

    @Test
    public void test5(){
        //筛选集合apples中状态为SUCCESSF并去重
        List<apple> list = apples.stream()
                .filter(a -> a.getStatus().equals(apple.status.SUCCESSF))
                .distinct()
                .collect(Collectors.toList());
       list.forEach(System.out::println);
    }
}
